using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebApplication
{
	/// <summary>
	/// Summary description for BuyPrivateCompany.
	/// </summary>
	public class BuyPrivateCompany : System.Web.UI.Page
	{
		private	bool	_bBuyer;
		private	bool	_bSeller;
		private	string	_sBuyer;
		private	string	_sSeller;
		
		protected System.Web.UI.WebControls.Label Title;
		protected System.Web.UI.WebControls.Label Status;
		protected System.Web.UI.WebControls.Label Label1;
		protected System.Web.UI.WebControls.Label Label2;
		protected System.Web.UI.WebControls.Label Label3;
		protected System.Web.UI.WebControls.Label Min;
		protected System.Web.UI.WebControls.Label Max;
		protected System.Web.UI.WebControls.TextBox Price;
		protected System.Web.UI.WebControls.Button Accept;
		protected System.Web.UI.WebControls.Button Reject;
		protected System.Web.UI.WebControls.CheckBox PriorityCheckBox;
		protected System.Web.UI.WebControls.LinkButton Back;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Back.Click += new System.EventHandler(this.Back_Click);
			this.Accept.Click += new System.EventHandler(this.Accept_Click);
			this.Reject.Click += new System.EventHandler(this.Reject_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			Game	game	= Session["Game"] as Game;
			string	sPlayer = Session["PlayerName"].ToString ();
			
			if (game.PrivateCompanyForSale == null)		// sale over or nothing for sale so return to SR
			{
				if (game.PrivateCompanySaleDuringStockRound)
					Response.Redirect("StockRound.aspx");
				else
					Response.Redirect("OperatingRound2.aspx");
			}
			
			int		nAmount		= game.PrivateCompanySalePrice;
			string	sCompany	= game.PrivateCompanyForSale.Name;
			bool	bPriority	= sPlayer == game.PrivateCompanyPriorityPlayerName;
			bool	bBidEntered	= nAmount > 0;

			_sBuyer		= game.PrivateCompanyBuyer.Name;
			_sSeller	= game.PrivateCompanySeller.Name;
			_bBuyer		= sPlayer == _sBuyer;
			_bSeller	= sPlayer == _sSeller;

			PriorityCheckBox.Checked = bPriority;
			
			Min.Text	= (game.PrivateCompanyForSale.Cost / 2).ToString();
			Max.Text	= (game.PrivateCompanyForSale.Cost * 2).ToString();
			
			if (_bBuyer)			// I am buying
			{
				if (nAmount == 0)	// no bid
					nAmount	= game.PrivateCompanyForSale.Cost;
				else
					nAmount	= game.PrivateCompanySalePrice;
				
				if (!IsPostBack)
					Price.Text	= nAmount.ToString();

				if (!game.PrivateCompanySold)	// offer not accepted
				{
					Status.Text = "Price you wish to pay for the " + sCompany + "?";
				
					Accept.Text = "Buy";
					Reject.Text = "Cancel";
					
					Accept.Enabled	= !bBidEntered;
					Reject.Enabled	= !bBidEntered;
					Price.Enabled	= !bBidEntered;
				}
				else
				{
					if (game.PrivateCompanySalePrice > 0)	// offer accepted ?
						Status.Text = "You bought the " + sCompany + " for $" + game.PrivateCompanySalePrice;
					else
						Status.Text = "You offer to buy the " + sCompany + " was rejected";
				
					Accept.Visible	= false;
					Reject.Text		= "Done";
					Price.Enabled	= false;
				}
			}
			else if (_bSeller)		// I am selling
			{
				Price.Text	= nAmount.ToString();

				if (!game.PrivateCompanySold)	// offer not accepted
				{
					Status.Text = _sBuyer + " wishes to buy the " + sCompany + " from you for $" + nAmount;
			
					Accept.Text = "Accept";
					Reject.Text = "Reject";

					Accept.Enabled = bBidEntered;
					Reject.Enabled = bBidEntered;
				}
				else
				{
					if (game.PrivateCompanySalePrice > 0)	// offer accepted ?
						Status.Text = "You sold the " + sCompany + " to " + _sBuyer + " for $" + nAmount;
					else
						Status.Text = "You rejected the offer to sell the " + sCompany + " to " + _sBuyer;
			
					Accept.Enabled = false;
					Reject.Enabled = false;
				}
				
				Price.Enabled = false;
			}
			else					// I am viewing
			{
				Price.Text	= nAmount.ToString();

				if (!game.PrivateCompanySold)	// offer not accepted
					Status.Text = _sBuyer + " is attempting to buy the " + sCompany + " from " + _sSeller + " for $" + nAmount;
				else if (game.PrivateCompanySalePrice > 0)
					Status.Text = _sBuyer + " bought the " + sCompany + " from " + _sSeller + " for $" + nAmount;
				else
					Status.Text = _sBuyer + "'s offer to buy the " + sCompany + " from " + _sSeller + " was rejected";

				Accept.Visible	= false;
				Reject.Visible	= false;
				Price.Enabled	= false;
			}
		}


		private void Accept_Click(object sender, System.EventArgs e)
		{
			Game	game	= Session["Game"] as Game;
			string	sPlayer = Session["PlayerName"].ToString ();

			if (_bBuyer)
			{
				int	nBid = int.Parse(Price.Text);

				if (nBid > 0)
				{
					Player	player	= game.Players[sPlayer];

					if (player.Cash >= nBid)
					{
						game.PrivateCompanySalePrice = nBid;
						game.PrivateCompanyPriorityPlayerName = _sSeller;
					
						PriorityCheckBox.Checked = false;
					}
					else
					{
						// TODO don't have enough cash
					}
				}
			}
			else if (_bSeller)
			{
				string	sMsg;

				sMsg = _sBuyer + " bought the " + game.PrivateCompanyForSale.NickName + " from " + _sSeller + " for $" + game.PrivateCompanySalePrice;
								
				game.History.Add (sMsg);

				// transfer the company and money
				Player	buyer	= game.Players[_sBuyer];
				Player	seller	= game.Players[_sSeller];

				buyer.BuyPrivateCompany(seller, game.PrivateCompanyForSale, game.PrivateCompanySalePrice);
				buyer.PurchasedSomething = true;

				game.PrivateCompanyPriorityPlayerName = _sBuyer;
				game.PrivateCompanySold = true;
				
				PriorityCheckBox.Checked = false;

				if (game.PrivateCompanySaleDuringStockRound)
					Response.Redirect("StockRound.aspx");
				else
					Response.Redirect("OperatingRound2.aspx");
			}
		}
		private void Reject_Click(object sender, System.EventArgs e)
		{
			Game	game	= Session["Game"] as Game;
			
			if (_bBuyer)
			{
				game.ResetPrivateSaleInfo();
			}
			else if (_bSeller)
			{
				game.PrivateCompanySalePrice = 0;	// tell him rejected
				game.PrivateCompanyPriorityPlayerName = _sBuyer;
				game.PrivateCompanySold = true;
				
				PriorityCheckBox.Checked = false;
			}

			if (game.PrivateCompanySaleDuringStockRound)
				Response.Redirect("StockRound.aspx");
			else
				Response.Redirect("OperatingRound2.aspx");
		}

		private void Back_Click(object sender, System.EventArgs e)
		{
			Game	game	= Session["Game"] as Game;

			PriorityCheckBox.Checked = false;

			if (game.PrivateCompanySaleDuringStockRound)
				Response.Redirect("StockRound.aspx");
			else
				Response.Redirect("OperatingRound2.aspx");
		}
	}
}
